package com.ww.scm.web;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jmesa.facade.TableFacade;
import org.jmesa.limit.ExportType;
import org.jmesa.view.html.component.HtmlColumn;
import org.jmesa.view.html.component.HtmlRow;
import org.jmesa.view.html.component.HtmlTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ww.core.component.CustomReportToolbar;
import com.ww.core.component.CustomReportView;
import com.ww.scm.constants.Constants;
import com.ww.scm.valueObject.grid.DataRow;
import com.ww.scm.valueObject.grid.GridView;
import com.ww.scm.valueObject.grid.HeadRow;

@Controller
@RequestMapping(value = "/report")
public class ReportController {
	
	/*
	 * 日志类
	 */
	private static Logger logger = LoggerFactory.getLogger(ReportController.class);

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	private static final String DEMO_SQL="SELECT * FROM t_pub_dic t where 1=1 ";
	
	@RequestMapping(params = "method=demo1")
	public ModelAndView demo1(HttpServletRequest request) {
		
		String typeId=request.getParameter("typeId");
		String[] para = {typeId};
		
		

		HeadRow headRow = new HeadRow();		
		headRow.setTrStyle("FixedTitleRowSpan");
		headRow.addActiveCell("标题1","header FixedTitleColumn");
		headRow.addActiveCell( "标题2","header FixedTitleColumn");
		headRow.addActiveCell( "标题3","header FixedTitleColumn");
		
		StringBuffer sqlBuffer=new StringBuffer(DEMO_SQL);
		List<DataRow> dataRows= jdbcTemplate.query(sqlBuffer.toString()/*, para*/, new RowMapper(){
			public Object mapRow(ResultSet res, int index) throws SQLException {
				DataRow dataRow=new DataRow();
				dataRow.addActiveCell(res.getString("F_NAME"),"dataRow FixedDataColumn");
				dataRow.addActiveCell(res.getString("F_CODE"),"dataRow");

				String id=res.getString("F_ID");
				if (StringUtils.isBlank(id)){
					id=Constants.EMPTY;
				}
				dataRow.addActiveCell("<a href='javascript:open(\"WeekBuyBackPlanDetailRpt.jsp?factoryId="+id+"\");'>回购详情</a>","dataRow");
				
				return dataRow;
			}
		});
		
		GridView gridView=new GridView();
		gridView.setTitle("demo1");
		gridView.addDataRows(dataRows);
		gridView.addHeadRow(headRow); 
		
		return new ModelAndView("/report/demo1", "gridView",gridView);
	}
	
	@RequestMapping(params = "method=demo2")
	public ModelAndView bill(HttpServletRequest request,HttpServletResponse response) {
		
		
		TableFacade tableFacade = new TableFacade("demo2_table", request, response);
		tableFacade.setMaxRows(10000);

		StringBuffer sqlBuffer=new StringBuffer(DEMO_SQL);
		List<Map<String, Object>> dataRows= jdbcTemplate.queryForList(sqlBuffer.toString());
		
		
		HtmlTable htmlTable = new HtmlTable();

		HtmlRow htmlRow = new HtmlRow();
		htmlTable.setRow(htmlRow);


		HtmlColumn billCode = new HtmlColumn("F_NAME").title("标题1");
		billCode.setFilterable(false);
		htmlRow.addColumn(billCode);

		HtmlColumn billDate = new HtmlColumn("F_CODE").title("标题2");
		billDate.setFilterable(false);
		htmlRow.addColumn(billDate);
		
        
        
        tableFacade.setTable(htmlTable);
        tableFacade.setItems(dataRows);
        tableFacade.setExportTypes(ExportType.EXCEL,ExportType.PDF);
		tableFacade.setToolbar(new CustomReportToolbar());
		tableFacade.setView(new CustomReportView());
       
		String reportTable = tableFacade.render();
		return new ModelAndView("/report/demo2", "reportTable",reportTable);
	}
	
	
    
	
}
